package com.newbsd.web.maventest.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.newbsd.web.maventest.common.pojo.EasyUIDataGridJSON;
import com.newbsd.web.maventest.dao.impl.UserDaoImpl;
import com.newbsd.web.maventest.mvc.InnerDispatcherController;
import com.newbsd.web.maventest.pojo.Ticket;
import com.newbsd.web.maventest.pojo.User;
import com.newbsd.web.maventest.service.UserQueryDao;
import com.newbsd.web.maventest.service.impl.UserQueryDaoImpl;

/**
 * @author wangjing
 *
 */
public class UserController extends InnerDispatcherController{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//对用户查询到的所有票进行分页
	public void pageDatagrid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String origin = request.getParameter("origin");
		String destination = request.getParameter("destination");
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		
		UserQueryDao uqd = new UserQueryDaoImpl();
		List<Ticket> ticketlist = uqd.QueryTickets(origin, destination,page,rows);

		//int totalRecords = uqd.QueryTicNum(origin, destination);
		
		request.setAttribute("ticketList", ticketlist);
		request.getRequestDispatcher("WEB-INF/view/utickets.jsp").forward(request,
				response);
		
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(ticketlist.size());
		responseObject.setRows(ticketlist);
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(ticketlist));
		System.out.println(JSON.toJSONString(ticketlist));
	}
	
	//对密码进行修改
	public void doPasswordEdit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession se = request.getSession();
		User user = (User) se.getAttribute("user");
		//User user = (User)request.getAttribute("user");
		
		
		String pwd = user.getPassword();
		String oldpwd = request.getParameter("oldpwd");
		String newpwd = request.getParameter("newpwd");
		String newpwd2 = request.getParameter("newpwd2");
//		System.out.println(pwd);
//		System.out.println(oldpwd);
//		System.out.println(newpwd);
//		System.out.println(newpwd2);
		
		//User use = new User();
		if(!pwd.equals(oldpwd)) {
			//request.getRequestDispatcher("pwdedit.jsp").forward(request, response);
			request.setAttribute("msg", "password is wrong!");
			request.getRequestDispatcher("/error.jsp").forward(request,
					response);
		}
		else if(!newpwd.equals(newpwd2)) {
			//request.getRequestDispatcher("pwdedit.jsp").forward(request, response);
			request.setAttribute("msg", "confirm your password!");
			request.getRequestDispatcher("/error.jsp").forward(request,
					response);
		}
		else {
			UserDaoImpl udi=new UserDaoImpl();
//			User nuser = new User();
//			nuser.setPassword(newpwd2);
//			udi.modUserPwd(nuser);
			user.setPassword(newpwd2);
			udi.modUserPwd(user);
			
			request.setAttribute("msg", "modify success");
			request.getRequestDispatcher("/success.jsp").forward(request,
					response);
//			int id = Integer.parseInt(request.getParameter("id"));
//			nuser.setId(id);
//			String username = user.getUsername();
//			System.err.println(username);
//			nuser.setUsername(username);
//			udi.modUserPwd(nuser);
			//request.getRequestDispatcher("pwdedit.jsp").forward(request, response);
		}

//		String msg = validate(use);
//		if (msg==null) {
//			if (MD5Servlet.md5(use.getPasswordOld()).equals(user.getPassword())) {
//				
//				use.setId(user.getId());
//				use.setPassword(MD5Servlet.md5(use.getPassword()));
//				UserDaoImpl udi = new UserDaoImpl();
//				//udi.updateUserInfo(use);
//
//				// 更新Session中User
//				user.setPassword(use.getPassword());
//
//				request.getRequestDispatcher("/User/main.jsp").forward(request,
//						response);
//			} else {
//				msg = "原密码不正确";
//				request.setAttribute("msg", msg);
//				request.getRequestDispatcher("pwdedit.jsp")
//						.forward(request, response);
//			}
//		} else {
//			request.setAttribute("msg", msg);
//			request.getRequestDispatcher("pwdedit.jsp")
//					.forward(request, response);
//		}
	}
	
	
//	private String validate(User user) {
//		String errorMsg = null;
//		if (user.getPasswordOld()==null) {
//			errorMsg = "请输入原密码";
//		} else if (user.getPassword()==null) {
//			errorMsg = "请输入新密码";
//		} else if (user.getPassword2()==null) {
//			errorMsg = "请输入确认密码";
//		} else if (!user.getPassword().equals(user.getPassword2())) {
//			errorMsg = "两次密码不相等";
//		}
//		return errorMsg;
//	}
}
